package com.sengled.cloud.biz.union.mgt.api.config;

import com.sengled.cloud.biz.union.mgt.common.util.FastJsonUtils;
import com.sengled.cloud.biz.union.mgt.model.response.ResponseMessage;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAuthenticationFaildHandler  implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        ResponseMessage res = new ResponseMessage();
        res.setMessageCode("1000");
        res.setInfo("account or password error.");
        res.setDescription("账号或密码错误。");
        httpServletResponse.setStatus(HttpStatus.OK.value());
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
        httpServletResponse.getWriter().println(FastJsonUtils.toJSONNoFeatures(res));
    }
}
